<?php
$customFields = Set::combine($customFields,'{n}.CustomField.id','{n}','{n}.CustomField.fieldset_id');
?>
<strong><?php echo __("Field Groups")?></strong>
<div id="fieldset_tree">
	<ul>
	<?php foreach($fieldsets as $fieldset){?>
		<li id="<?php echo 'fieldset_'.$fieldset['Fieldset']['id']?>" class="fieldset_elem">
			<?php if(strlen($fieldset['Fieldset']['label']))
			{
				$label =  $fieldset['Fieldset']['label'].' ('.$fieldset['Fieldset']['name'].')';
			}else
			{
				$label =  $fieldset['Fieldset']['name'];
			}
				echo $html->link($label,'#');
			?>
			<?php 
				$fielsetFields = @$customFields[$fieldset['Fieldset']['id']];
				if($fielsetFields && count($fielsetFields))
				{?>
					<ul>
						<?php foreach($fielsetFields as $customField) {?>
							<li id="customfield_<?php echo $customField['CustomField']['id']?>" class="custom_field_elem">
								<?php echo $html->link($customField['CustomField']['label'].'('.$customField['CustomField']['name'].')','#')?>
							</li>
						<?php } ?>
					</ul>
			<?php }
			?>
		</li>
	<?php  } Configure::write("debug",0)?>
	</ul>
</div>
<script>
	$('#fieldset_tree').jstree({ "plugins" : [ "themes", "html_data", "checkbox", "sort", "ui" ],
		'checkbox' :{'two_state':false}});
</script>